package com.blue.utils;

import java.io.ByteArrayOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
	
	public StringUtils(){
		
	}
	/**
	 * 字符串中的空格、回车、换行符、制表符
	 * 注：\n 回车(\u000a) \t 水平制表符(\u0009) \s 空格(\u0008) \r 换行(\u000d)
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {
		String dest = "";
		if (str!=null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}
	
	
	
	/**
	 * 判断字符串为数字
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str){
	    Pattern pattern = Pattern.compile("[0-9]*");
	    return pattern.matcher(str).matches();   
	}
	
	public static boolean isString(String str){
	    Pattern pattern = Pattern.compile("[a-zA-Z]*");
	    return pattern.matcher(str).matches();   
	}
	
	public static boolean isWindowPath(String str){
		
		str=str.replaceAll("/", "\\\\");
		//Pattern p = Pattern.compile("^[a-zA-Z]:(\\\\[^\\\\/<>|:*?\"]+)+(/$)?");
		Pattern p = Pattern.compile("^[a-zA-Z]:(\\\\[^\\\\/<>|:*?\"]+)+$");
		return p.matcher(str).matches();
	}
	
	public static String analysisNumeric(String inputStr){
		Pattern p=Pattern.compile("[^0-9]");   
		Matcher m=p.matcher(inputStr);       
		return m.replaceAll("").trim();
	}
	
	public static void main(String[] args) {
		
		System.out.println( StringUtils.replaceBlank("just do \n it!"));
		/***
		System.out.println(encode("中"));
		System.out.println(decode(encode("中")));
		System.out.println(encode("123456"));
		
		String str="中文W";
		for(int i=0;i<str.length();i++){
			System.out.println(str.charAt(i));
		}
		char c=(char)str.charAt(0);
		System.out.println(c+"转换为16进制为："+Integer.toString(c,16));
		System.out.println("16进制转换为字符串为:"+(char)Integer.valueOf(Integer.toString(c, 16),16).intValue());
		
		String num = "FF";
		int parseInt = Integer.parseInt(num, 16);
		System.out.println(parseInt);
		
		int temp = 1234567;
		String hexString = Integer.toHexString(temp);
		System.out.println( "hexString = " + hexString );
		String str2 = Integer.toString(Integer.parseInt(hexString, 16));
		System.out.println( "str = " + str2 );
		
		String strer="中";
		char cer=(char)str.charAt(0);
		System.out.println(cer+"转换为2进制为："+Integer.toString(cer,2));
		
		**********************************************************************/
		 
		char change_buf[];
		
		String test = "901532\0\0\0\0\0\0\0\0\0ICP20130218111623";
		
		byte[] newByte = test.getBytes();

		change_buf = test.toCharArray();
		
		for(int i=0;i<change_buf.length;i++){
			
			byte[] byteStr = charToByte(change_buf[i]);
			
			System.out.println(change_buf[i]+" change Byte [] ::["+byteStr+"]"+" --- ::"+byteToChar(byteStr)+" ---::"+newByte[i]);
			
		}
		
		printHexString(18,newByte);
		
		if(isNumeric("123  ".trim())){
			System.out.println(" error");
		}
		StringUtils teststr = new StringUtils();
		teststr.changeStr("Tables      1 (5)");
		
		String newStr[]="   Tables      1 (5)".split("\\s{2,}");
		System.out.println(newStr.length);
		System.out.println(newStr[0]);
		System.out.println(newStr[1]);
		System.out.println(newStr[2]);
		System.out.println(newStr[newStr.length-1]);
		
		
		teststr.testads();
		
		
		String ss="s0-110www0013.sql";
		  Pattern p=Pattern.compile("[^0-9]");   
		  Matcher m=p.matcher(ss);       
		  System.out.println( m.replaceAll("").trim());
		  
		  if(isNumeric("123".trim())){
				System.out.println(" error");
			}
		  if(isString("jfjfjfjjfDDDDDDDDD   ".trim())){
				System.out.println(" String ok");
			}
		  
		  String checkPath="c:/e7777st/tttt/jh";
		  
		  checkPath=checkPath.replaceAll("/", "\\\\");
		  //checkPath=checkPath.replaceAll("\\\\", "\\\\");
		  
		  System.out.println(checkPath);
		  
		  if(isWindowPath(checkPath.trim())){
			  System.out.println(" path String ok");
		  }
	}
	
	public void testads(){
	    //Pattern pattern = Pattern.compile("（(.*?)）"); //中文括号
		Pattern pattern = Pattern.compile("\\((.*?)%"); //英文括号
	    String s = "行政专员(袁文全%)"; //中文括号
	    Matcher matcher = pattern.matcher(s);
	    if (matcher.find()){
	    	//System.out.println(matcher.group(0));
	        System.out.println(matcher.group(1));
	    }
	}
	
	
	
	public void changeStr(String inStr){
		Pattern p = Pattern.compile("\\s{2,}");
		Matcher m = p.matcher(inStr);
		while(m.find())
		{
			//System.out.println("----"+m.group(0));
		}
	}
	
	/***
		1.string 转 byte[]
			byte[] midbytes=isoString.getBytes("UTF-8"); //为UTF8编码
			byte[] isoret = srt2.getBytes("ISO-8859-1");//为ISO-8859-1编码 其中ISO-8859-1为单字节的编码
		2.byte[]转string
			String isoString = new String(bytes,"ISO-8859-1");
			String srt2=new String(midbytes,"UTF-8");
	 */
	
	public static byte[] charToByte(char c) {
        byte[] b = new byte[2];
        b[0] = (byte) ((c & 0xFF00) >> 8);
        b[1] = (byte) (c & 0xFF);
        return b;
    }
	
	public static char byteToChar(byte[] b) {
        char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
        return c;
    }
	
	/** 
	* 将指定byte数组以16进制的形式打印到控制台 
	* @param hint String 
	* @param b byte[] 
	* @return void 
	*/ 
	public static void printHexString(int countNo,byte[] b) { 
		
		for (int i = 0; i < b.length; i++) { 
			if ( (i%countNo) == 0){
				System.out.println("");
			}
			String hex = Integer.toHexString(b[i] & 0xFF); 
			if (hex.length() == 1) { 
				hex = '0' + hex; 
			} 
			System.out.print(hex.toUpperCase() + " "); 
		} 
		System.out.println(""); 
		
		//String test = new String(b);
		//System.out.println("---"+ test); 
	} 
	
	/** 
	* 
	* @param b byte[] 
	* @return String 
	*/ 
	public static String Bytes2HexString(byte[] b) { 
		String ret = ""; 
		for (int i = 0; i < b.length; i++) { 
			String hex = Integer.toHexString(b[i] & 0xFF); 
			if (hex.length() == 1) { 
				hex = '0' + hex; 
			} 
			ret += hex.toUpperCase(); 
		} 
		return ret; 
	} 

	/** 
	* 将两个ASCII字符合成一个字节； 
	* 如："EF"--> 0xEF 
	* @param src0 byte 
	* @param src1 byte 
	* @return byte 
	*/ 
	public static byte uniteBytes(byte src0, byte src1) { 
		byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue(); 
		_b0 = (byte)(_b0 << 4); 
		byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue(); 
		byte ret = (byte)(_b0 ^ _b1); 
		return ret; 
	} 

	/** 
	* 将指定字符串src，以每两个字符分割转换为16进制形式 
	* 如："2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9} 
	* @param src String 
	* @return byte[] 
	*/ 
	public static byte[] HexString2Bytes(String src){ 
		byte[] ret = new byte[8]; 
		byte[] tmp = src.getBytes(); 
		for(int i=0; i<8; i++){ 
			ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); 
		} 
		return ret; 
	} 
	
	/*
	 * 16进制数字字符集
	 */
	private static String hexString="0123456789ABCDEF";
	/*
	 * 将字符串编码成16进制数字,适用于所有字符（包括中文）
	 */
	public static String encode(String str)
	{
		//根据默认编码获取字节数组
		byte[] bytes=str.getBytes();
		StringBuilder sb=new StringBuilder(bytes.length*2);
		//将字节数组中每个字节拆解成2位16进制整数
		for(int i=0;i<bytes.length;i++)
		{
			sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
			sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
		}
		return sb.toString();
	}
	/*
	 * 将16进制数字解码成字符串,适用于所有字符（包括中文）
	 */
	public static String decode(String bytes)
	{
		ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
		//将每2位16进制整数组装成一个字节
		for(int i=0;i<bytes.length();i+=2)
			baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
		return new String(baos.toByteArray());
	}





}
